package com.company.project.controller.wx.demo;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;


@RestController()
@RequestMapping("/wxdemo/msg")
public class WxTemplateMsgController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMpService wxService;



    @RequestMapping("test")
    @ResponseBody
    public void templateMessage() throws Exception {
        System.out.println("accessToken:" + this.wxService.getAccessToken());
//        testSetIndustry();
//        testGetIndustry();
//        testAddTemplate();
//        testGetAllPrivateTemplate();
        testSendTemplateMsg();

    }

    public void testSetIndustry() throws Exception {
        WxMpTemplateIndustry industry = new WxMpTemplateIndustry(new WxMpTemplateIndustry.Industry("1"),
                new WxMpTemplateIndustry.Industry("04"));
        boolean result = this.wxService.getTemplateMsgService().setIndustry(industry);
        System.out.println("testSetIndustry:" + result);
    }

    public void testGetIndustry() throws Exception {
        final WxMpTemplateIndustry industry = this.wxService.getTemplateMsgService().getIndustry();
        System.out.println(industry);
    }

    public void testAddTemplate() throws Exception {
        String result = this.wxService.getTemplateMsgService().addTemplate("TM00008");
        System.err.println(result);
    }

    public void testGetAllPrivateTemplate() throws Exception {
        List<WxMpTemplate> result = this.wxService.getTemplateMsgService().getAllPrivateTemplate();
        System.err.println(result);
    }

    public void testSendTemplateMsg() throws WxErrorException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("otxJT06yNLTNJTDQ6yu0ugHcoScg")
                .templateId("VKgjTYmLYuXOfshUZyGVuVV4It1LQHgAvhNlFyVuUzQ").build();
        templateMessage.addWxMpTemplateData(new WxMpTemplateData("first", "恭喜你购买成功", "#FF00FF"));
        templateMessage.addWxMpTemplateData(new WxMpTemplateData("remark", "欢迎再来", "#FF00FF"));
        templateMessage.setUrl(" ");
        String msgId = this.wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        System.out.println(msgId);
    }


}
